package com.cloud.spider.threadtasks;


import com.cloud.spider.clients.QiZhouBookClient;
import com.cloud.spider.dtos.ChapterContentDto;
import com.cloud.spider.dtos.ChapterLinkDto;
import com.cloud.spider.utils.FreemarkerUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author 柳晓宇
 * @description 奇州小说网爬虫线程任务
 * @create 2023/9/3
 */
public class QiZhouBookCrawlerRunnable implements Runnable{

    private ChapterLinkDto link;

    private QiZhouBookClient qiZhouBookClient;

    private List<ChapterContentDto> chapterContentList;

    private CountDownLatch countDownLatch;

    public static final String PREFIX_URL = "https://www.qzjpxh.com";

    public QiZhouBookCrawlerRunnable() {
    }

    public QiZhouBookCrawlerRunnable(ChapterLinkDto link, QiZhouBookClient qiZhouBookClient, List<ChapterContentDto> chapterContentList, CountDownLatch countDownLatch) {
        this.link = link;
        this.qiZhouBookClient = qiZhouBookClient;
        this.chapterContentList = chapterContentList;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            // 正文
            StringBuilder stringBuilder = new StringBuilder(1024);
            // uri
            String nextUri = link.getUri();
            ChapterContentDto chapterContentDto = new ChapterContentDto();
            // 提前存入uri，在下方会存入下一章的uri
            chapterContentDto.setUri(nextUri);
            do {
                String url = PREFIX_URL + nextUri;
                String contentString = qiZhouBookClient.http(url);
                Document contentDocument = Jsoup.parse(contentString);
                Element chapterContent = contentDocument.getElementById("chaptercontent");
                if (chapterContent == null) {
                    return;
                }
                Element linkNextElement = contentDocument.getElementById("linkNext");
                nextUri = linkNextElement.attr("href");
                String contentHtml = chapterContent.html();
                stringBuilder.append(contentHtml);
            } while (nextUri.contains("_"));

            chapterContentDto.setTitle(link.getTitle());
            chapterContentDto.setChapterContent(stringBuilder.toString());
            // 处理无效字符
            chapterContentDto = FreemarkerUtil.removeIrrelevantText(chapterContentDto);

            chapterContentList.add(chapterContentDto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 计数器减一
            countDownLatch.countDown();
        }

    }
}
